package com.own.business.resource.core.model.base;

import com.own.business.resource.core.model.base.method.FolderMethod;
import com.own.business.resource.core.util.FileOnceUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * UploadModel
 *
 * @author chenxueli
 * @date 2023-07-07 15:32:27
 */
@Slf4j
@Getter
public abstract class BaseUploadModel implements FolderMethod {

    /**
     * 是否已经上传
     */
    public boolean upload;
    /**
     * 上传的文件名称
     */
    private String fileName;
    /**
     * 上传的文件名称（原名称）
     */
    @Setter
    private String oldFileName;
    /**
     * 上传的文件类型
     */
    private String contentType;
    /**
     * 文件后缀（格式）
     */
    private String suffix;
    /**
     * 文件大小（单位Byte）
     */
    @Setter
    private long fileSize;
    /**
     * 文件md5信息（摘要信息）
     */
    @Setter
    private String md5;
    /**
     * 路径单元列表
     */
    private List<String> folderList;
    /**
     * 上传的位置
     */
    @Setter
    private String position;
    /**
     * 文件流信息
     */
    private InputStream inputStream;

    public BaseUploadModel() {
        this.folderList = new ArrayList<>();
    }

    /**
     * 设置文件流信息
     *
     * @return 文件流信息
     */
    public abstract InputStream setInputStream();

    /**
     * 随机文件名
     */
    public final void randomName() {
        this.fileName = UUID.randomUUID().toString().replaceAll("-", "") + this.suffix;
    }

    /**
     * 初始化基本信息
     */
    public final void init() {
        this.folderList = new ArrayList<>();
        this.fileName = this.oldFileName;
        this.suffix = FileOnceUtil.getSuffix(this.getFileName());
        this.contentType = MediaTypeFactory.getMediaType(this.getFileName()).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
    }

    /**
     * 获取文件流信息
     *
     * @return 文件流信息
     */
    public final InputStream getInputStream() {
        if (inputStream == null) {
            return setInputStream();
        }
        return inputStream;
    }

    /**
     * 关闭并清空文件流信息
     */
    public final void close() {
        if (inputStream == null) {
            return;
        }
        try {
            inputStream.close();
        } catch (IOException e) {
            log.error("文件流关闭失败", e);
        } finally {
            inputStream = null;
        }
    }

    /**
     * 设置上传成功属性
     */
    public void success() {
        this.upload = true;
    }

    /**
     * 设置基础信息
     *
     * @param model 基础信息
     */
    protected void setInfo(BaseUploadModel model) {
        this.upload = model.isUpload();
        this.fileName = model.getFileName();
        this.oldFileName = model.getOldFileName();
        this.contentType = model.getContentType();
        this.suffix = model.getSuffix();
        this.fileSize = model.getFileSize();
        this.md5 = model.getMd5();
        this.folderList = model.getFolderList();
        this.position = model.getPosition();
        this.inputStream = model.getInputStream();
    }

}
